package com.fhj.bookshop.service.impl;

import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fhj.bookshop.entity.Customer;
import com.fhj.bookshop.entity.Myorder;
import com.fhj.bookshop.entity.OrderDTO;
import com.fhj.bookshop.entity.Product;
import com.fhj.bookshop.mapper.CustomerMapper;
import com.fhj.bookshop.mapper.MyorderMapper;
import com.fhj.bookshop.mapper.ProductMapper;
import com.fhj.bookshop.service.IMyorderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fu
 * @since 2023-12-12
 */
@Service
public class MyorderServiceImpl extends ServiceImpl<MyorderMapper, Myorder> implements IMyorderService {

    @Autowired
    MyorderMapper myorderMapper;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    CustomerMapper customerMapper;


    @Override
    public void updateOrder(String alipayNumber, String orderNumber,String updateTime) {
        LambdaQueryWrapper<Myorder> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Myorder::getOrderNumber,orderNumber);
        Myorder order=myorderMapper.selectOne(wrapper);
        order.setAlipayNumber(alipayNumber);
        order.setOrderUpdateTime(updateTime);
        order.setStatus("已支付");
        myorderMapper.update(order,wrapper);
        LambdaQueryWrapper<Product> productWrapper=new LambdaQueryWrapper<>();
        productWrapper.eq(Product::getProId,order.getProId());
        Product product=productMapper.selectOne(productWrapper);
        Integer num=product.getProInventory();
        num=num-order.getProNumber();
        product.setProInventory(num);
        productMapper.updateById(product);
        System.out.println("更新成功");
    }

    @Override
    public Myorder createOrder(Integer custId, Integer proId, Integer proNumber) {
        Myorder order=new Myorder();
        order.setCustId(custId);
        order.setProId(proId);
        order.setProNumber(proNumber);
        order.setStatus("未支付");
        LambdaQueryWrapper<Product> productWrapper=new LambdaQueryWrapper<>();
        productWrapper.eq(Product::getProId,proId);
        Product product=productMapper.selectOne(productWrapper);
        BigDecimal proPrice=product.getProPrice();
        BigDecimal orderPrice=proPrice.multiply(BigDecimal.valueOf(proNumber));
        order.setOrderPrice(orderPrice);
        String orderNumber=UUID.randomUUID().toString().replace("-","");
        order.setOrderNumber(orderNumber);
        myorderMapper.insert(order);
        return order;
    }

    @Override
    public OrderDTO findByOrderNumber(String orderNumber) {
        LambdaQueryWrapper<Myorder> orderWrapper=new LambdaQueryWrapper<>();
        orderWrapper.eq(Myorder::getOrderNumber,orderNumber);
        Myorder order=myorderMapper.selectOne(orderWrapper);
        LambdaQueryWrapper<Product> productWrapper=new LambdaQueryWrapper<>();
        productWrapper.eq(Product::getProId,order.getProId());
        Product product=productMapper.selectOne(productWrapper);
        LambdaQueryWrapper<Customer> customerWrapper=new LambdaQueryWrapper<>();
        customerWrapper.eq(Customer::getCustId,order.getCustId());
        Customer customer=customerMapper.selectOne(customerWrapper);
        OrderDTO orderDTO=new OrderDTO();
        orderDTO.setOrderNumber(orderNumber);
        orderDTO.setOrderPrice(product.getProPrice());
        orderDTO.setCustAccount(customer.getCustAccount());
        orderDTO.setProName(product.getProName());
        orderDTO.setProNumber(order.getProNumber());
        orderDTO.setProPrice(product.getProPrice());
        orderDTO.setOrderPrice(order.getOrderPrice());
        orderDTO.setOrderCreateTime(order.getOrderCreateTime());
        orderDTO.setStatus(order.getStatus());
        return orderDTO;
    }
    @Override
    public Page<Myorder> orderPageService(int pageCode, int pageSize,int custId){
        LambdaQueryWrapper<Myorder> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Myorder::getCustId,custId);
        Page<Myorder> page =new Page<Myorder>(pageCode,pageSize);
        myorderMapper.selectPage(page,wrapper);
        return page;
    }

    @Override
    public Page<Myorder> AdminOrderPageService(int pageCode, int pageSize){
        Page<Myorder> page =new Page<Myorder>(pageCode,pageSize);
        myorderMapper.selectPage(page,null);
        return page;
    }
}
